package com.example.ssm1.interceptors;

import com.example.ssm1.model.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("LoginUser");
        if(loginUser==null){
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }

        return super.preHandle(request, response, handler);
    }
}
